package com.pfzy.liveeventbusdemo.activity

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.annotation.Nullable
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * Ipc测试
 */
class IpcTestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LiveEventBus
            .get(KEY_TEST_IPC_OBSERVE, Any::class.java)
            .observe(this, object : Observer<Any> {
                override fun onChanged(o: Any) {
                    Toast.makeText(this@IpcTestActivity, o.toString() + "", Toast.LENGTH_SHORT).show()
                    val intent = Intent()
                    intent.putExtra(IpcTestActivity.Companion.RESULT_EXTRA_KEY, o.toString() + "")
                    setResult(IpcTestActivity.Companion.RESULT_CODE, intent)
                    finish()
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    companion object {
        const val KEY_TEST_IPC_OBSERVE = "key_test_ipc_observe"
        const val RESULT_CODE = 0
        const val RESULT_EXTRA_KEY = "result_extra_key"
    }
}